//
//  TMAliPayManger.m
//  WorthWhile
//
//  Created by Mac on 2018/6/22.
//  Copyright © 2018年 stoneobs.icloud.com. All rights reserved.
//

#import "TMAliPayManger.h"
@interface  TMAliPayManger()
@property(nonatomic, copy) TMAliPayMangerPayHandle                     handle;/**< <##> */
@end
@implementation TMAliPayManger
+ (TMAliPayManger *)manger{
    static TMAliPayManger * manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = TMAliPayManger.new;
    });
    return manger;
}
- (void)inAppDelegateDealOpenURL:(NSURL *)url{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            //NSLog(@"result = %@",resultDic);
            NSString * status = resultDic[@"resultStatus"];
            NSString * alert = [self hudFromCode:status];
            if ([status isEqualToString:@"9000"]) {
                if (self.handle) {
                    self.handle(YES, alert);
                }
            }else{
                if (self.handle) {
                    self.handle(NO, alert);
                }
            }
        }];
    }
}
- (void)startPayOrder:(NSString *)order handle:(TMAliPayMangerPayHandle)handle{
    self.handle = handle;
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *executableFile = [infoDictionary objectForKey:(NSString *)kCFBundleExecutableKey]; //获取项目名称
    [[AlipaySDK defaultService] payOrder:order fromScheme:executableFile callback:^(NSDictionary *resultDic) {
        //NSLog(@"reslut = %@",resultDic);
        //回调在appdelegate 中
    }];
}
//处理支付宝
- (NSString*)hudFromCode:(NSString*)code{
    if ([code isEqualToString:@"9000"]) {
        return @"支付成功";
    }
    if ([code isEqualToString:@"8000"]) {
        return @"正在处理中";
    }
    if ([code isEqualToString:@"4000"]) {
        return @"订单支付失败";
    }
    if ([code isEqualToString:@"5000"]) {
        return @"重复请求，支付失败";
    }
    if ([code isEqualToString:@"6001"]) {
        return @"中途取消，支付失败";
    }
    if ([code isEqualToString:@"6002"]) {
        return @"网络连接错误";
    }
    if ([code isEqualToString:@"6004"]) {
        return @"支付结果未知";
    }
    return @"未知错误";
}
@end
